package cn.lml.lession.auth.config;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import cn.lml.lession.common.constant.Constant;
import cn.lml.lession.common.util.Result;

import com.fasterxml.jackson.databind.ObjectMapper;

@Component("customAccessDeniedHandler")
public class CustomAccessDeniedHandler implements AccessDeniedHandler{

	@Autowired
	private ObjectMapper objectMapper;
	
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response,
			AccessDeniedException accessDeniedException) throws IOException, ServletException {
       response.setCharacterEncoding(Constant.UTF8);
       response.setContentType(Constant.CONTENT_TYPE);
       response.setStatus(HttpStatus.SC_FORBIDDEN);
       PrintWriter printWriter = response.getWriter();
       printWriter.append(objectMapper.writeValueAsString(Result.error("授权失败，禁止访问")));
	}

}
